package com.lzx.iteam;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.lzx.iteam.adapter.CRMTextAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.CRMTextBean;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.Constants;

import java.util.ArrayList;

import static android.R.id.edit;
import static android.R.id.message;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 08月 03日
 * Time: 下午1:32
 *
 * @QQ : 1234567890
 * **********************************
 */
public class CRMTextSelectActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.weekly_date_lv)
    private ListView mListView;

    private static final int CRM_CHANCE_TO_CLIENT = 10001;     // 机会转客户


    /**
     * action:
     * 2:跟进状态
     * 3:性别
     * 5:线索来源
     * 7(没有赢单)、13、16(有赢单):阶段
     * 9:机会类型
     * 11:机会来源
     * 14:行业
     * 15:客户级别
     */
    private int mFlag;
    private int mIndex = -1;

    /**
     * 2:跟进状态
     */
    public static String[] status = {"未处理", "已联系", "关闭"};
    /**
     * 3:性别
     */
    public static String[] sex = {"请选择", "男", "女"};
    /**
     * 5:线索来源
     */
    public static String[] source = {"其他", "广告", "研讨会", "搜索引擎", "客户介绍"};
    /**
     * 7:销售阶段
     */
    public static String[] stage = {"请选择", "初步接洽", "需求确定", "方案报价", "谈判审核", "赢单"};
    /**
     * 13:销售阶段
     */
    public static String[] stage_num = {"", "10%", "30%", "60%", "80%", "100%"};
    /**
     * 9:机会类型
     */
    public static String[] type = {"新用户", "老用户"};
    /**
     * 11:机会来源
     */
    public static String[] chance_origin = {"其他", "公海库", "线索库", "老客户", "自建"};

    /**
     * 14:行业
     */
    public static String[] industry = {"其他","金融", "政府", "咨询", "娱乐", "媒体", "零售", "能源", "服务", "制造业", "高科技", "教育", "电信", "公用事业", "非盈利事业"};

    /**
     * 15:客户级别
     */
    public static String[] client_level = {"重点", "普通", "非优先"};


    public static String[] origin = {"公海库", "线索库", "机会库", "客户库"};

    private CRMTextAdapter mAdapter;
    private ArrayList<CRMTextBean> mListData = new ArrayList<CRMTextBean>();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.weekly_date_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvRight.setText("完成");
        Intent intent = getIntent();
        mFlag = intent.getIntExtra("action_select", 0);
        mIndex = intent.getIntExtra("index", -1);
        mListData.clear();
        if (mFlag == 2) {
            mTvTitle.setText("跟进状态");
            for (int i = 0; i < status.length; i++) {
                mListData.add(new CRMTextBean(i, status[i]));
            }
        } else if (mFlag == 3) {
            mTvTitle.setText("性别");
            for (int i = 1; i < sex.length; i++) {
                mListData.add(new CRMTextBean(i, sex[i]));
            }
        } else if (mFlag == 5) {
            mTvTitle.setText("线索来源");
            for (int i = 0; i < source.length; i++) {
                mListData.add(new CRMTextBean(i, source[i]));
            }
        } else if (mFlag == 7) {
            mTvTitle.setText("销售阶段");
            for (int i = 1; i < stage.length - 1; i++) {
                mListData.add(new CRMTextBean(i, stage[i]));
            }
        } else if (mFlag == 9) {
            mTvTitle.setText("机会类型");
            for (int i = 0; i < type.length; i++) {
                mListData.add(new CRMTextBean(i, type[i]));
            }

        } else if (mFlag == 11) {
            mTvTitle.setText("机会来源");
            for (int i = 1; i < chance_origin.length; i++) {
                mListData.add(new CRMTextBean(i, chance_origin[i]));
            }

        } else if (mFlag == 14) {
            mTvTitle.setText("行业");
            for (int i = 1; i < industry.length; i++) {
                mListData.add(new CRMTextBean(i, industry[i]));
            }

        } else if (mFlag == 15) {
            mTvTitle.setText("客户级别");
            for (int i = 0; i < client_level.length; i++) {
                mListData.add(new CRMTextBean(i, client_level[i]));
            }

        } else if (mFlag == 16) {
            mTvTitle.setText("销售阶段");
            for (int i = 1; i < stage.length; i++) {
                mListData.add(new CRMTextBean(i, stage[i]));
            }
        }
        mAdapter = new CRMTextAdapter(mActivity, mListData);
        mListView.setAdapter(mAdapter);

        if (mFlag == 2 ||  mFlag == 5 || mFlag == 9 || mFlag == 15) {
            mAdapter.updateView(mIndex);
        } else {
            mAdapter.updateView(mIndex - 1);
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mFlag == 16 && "赢单".equals(mListData.get(position).getContent())){
                    mDialogUtil.titleDeleteMsgStyle("提示","确定", "取消", getResources().getString(R.string.CRM_turn_client));
                    mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
                        @Override
                        public void onConfirmClick(Dialog lDialog) {
                            Intent turn = new Intent(mActivity,CRMCreateEditActivity.class);
                            turn.putExtra("action_type", 7);
                            turn.putExtra("type", Constants.CRM_CHANCE);
                            turn.putExtra("group_id", getIntent().getStringExtra("group_id"));
                            turn.putExtra("chance_id", getIntent().getStringExtra("chance_id"));
                            turn.putExtra("chance_name", getIntent().getStringExtra("chance_name"));
                            startActivityForResult(turn,CRM_CHANCE_TO_CLIENT);
                            lDialog.dismiss();
                        }

                        @Override
                        public void onCancelClick(Dialog lDialog) {
                            lDialog.dismiss();
                        }
                    });
                }else{
                    mIndex = position;
                    mAdapter.updateView(mIndex);
                }
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                Intent intent = new Intent();
                if (mFlag == 2 || mFlag == 5 || mFlag == 9 || mFlag == 15) {
                    intent.putExtra("index", mIndex);
                } else {
                    intent.putExtra("index", mIndex + 1);
                }
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            Intent intent = new Intent();
            intent.putExtra("index", 5);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
